#!/bin/bash

# Docker Linux 构建脚本
# 在Docker容器中构建Linux版本的sokit-cli

set -e

echo "=========================================="
echo "Docker Linux 构建脚本"
echo "=========================================="

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

print_info() {
    echo -e "${BLUE}[INFO]${NC} $1"
}

print_success() {
    echo -e "${GREEN}[SUCCESS]${NC} $1"
}

print_warning() {
    echo -e "${YELLOW}[WARNING]${NC} $1"
}

print_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

# 检查Docker
check_docker() {
    if ! command -v docker &> /dev/null; then
        print_error "Docker未安装，请先安装Docker"
        exit 1
    fi
    
    if ! docker info &> /dev/null; then
        print_error "Docker服务未运行，请启动Docker服务"
        exit 1
    fi
    
    print_success "Docker检查通过"
}

# 选择Linux发行版和架构
select_distro() {
    echo "请选择Linux发行版:"
    echo "1) Ubuntu 20.04"
    echo "2) Ubuntu 22.04"
    echo "3) CentOS 8"
    echo "4) CentOS 9"
    echo "5) Arch Linux"
    echo "6) Debian 11"
    echo "7) 自定义"
    
    read -p "请输入选择 (1-7): " choice
    
    echo ""
    echo "请选择目标架构:"
    echo "1) x86_64 (64位)"
    echo "2) x86 (32位)"
    echo "3) ARM64"
    echo "4) ARM32"
    
    read -p "请输入架构选择 (1-4): " arch_choice
    
    case $arch_choice in
        1)
            TARGET_ARCH="x86_64"
            CMAKE_ARCH="x86_64"
            ;;
        2)
            TARGET_ARCH="i686"
            CMAKE_ARCH="i686"
            ;;
        3)
            TARGET_ARCH="aarch64"
            CMAKE_ARCH="aarch64"
            ;;
        4)
            TARGET_ARCH="arm"
            CMAKE_ARCH="arm"
            ;;
        *)
            print_error "无效架构选择"
            exit 1
            ;;
    esac
    
    case $choice in
        1)
            DISTRO="ubuntu:20.04"
            PKG_MANAGER="apt"
            PKG_INSTALL="apt update && apt install -y"
            QT_PKG="qt6-base-dev qt6-tools-dev"
            ;;
        2)
            DISTRO="ubuntu:22.04"
            PKG_MANAGER="apt"
            PKG_INSTALL="apt update && apt install -y"
            QT_PKG="qt6-base-dev qt6-tools-dev"
            ;;
        3)
            DISTRO="centos:8"
            PKG_MANAGER="yum"
            PKG_INSTALL="yum install -y"
            QT_PKG="qt6-qtbase-devel qt6-qttools-devel"
            ;;
        4)
            DISTRO="quay.io/centos/centos:stream9"
            PKG_MANAGER="dnf"
            PKG_INSTALL="dnf install -y"
            QT_PKG="qt6-qtbase-devel qt6-qttools-devel"
            ;;
        5)
            DISTRO="archlinux:latest"
            PKG_MANAGER="pacman"
            PKG_INSTALL="pacman -S --noconfirm"
            QT_PKG="qt6-base qt6-tools"
            ;;
        6)
            DISTRO="debian:11"
            PKG_MANAGER="apt"
            PKG_INSTALL="apt update && apt install -y"
            QT_PKG="qt6-base-dev qt6-tools-dev"
            ;;
        7)
            read -p "请输入Docker镜像名称 (例如: ubuntu:20.04): " DISTRO
            read -p "请输入包管理器 (apt/yum/pacman): " PKG_MANAGER
            read -p "请输入安装命令: " PKG_INSTALL
            read -p "请输入Qt6包名称: " QT_PKG
            ;;
        *)
            print_error "无效选择"
            exit 1
            ;;
    esac
    
    print_info "选择的发行版: $DISTRO"
    print_info "目标架构: $TARGET_ARCH"
}

# 构建Docker镜像
build_docker_image() {
    print_info "构建Docker镜像..."
    
    # 创建Dockerfile
    cat > Dockerfile.build << EOF
FROM $DISTRO

# 设置环境变量
ENV DEBIAN_FRONTEND=noninteractive
ENV QT_QPA_PLATFORM=minimal

# 安装依赖
RUN $PKG_INSTALL cmake g++ make git $QT_PKG

# 设置工作目录
WORKDIR /workspace

# 复制源代码
COPY . .

# 设置构建脚本权限
RUN chmod +x build_linux_package.sh

# 构建项目
RUN cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_SYSTEM_PROCESSOR=$CMAKE_ARCH ../src/sokit-cli && \
    make -j$(nproc) && \
    mkdir -p sokit-cli-linux-$TARGET_ARCH && \
    cp sokit-cli sokit-cli-linux-$TARGET_ARCH/ && \
    tar -czf sokit-cli-linux-$TARGET_ARCH-$(date +%Y%m%d).tar.gz sokit-cli-linux-$TARGET_ARCH/

# 创建输出目录
RUN mkdir -p /output

# 复制构建结果
RUN cp sokit-cli-linux-*.tar.gz /output/ 2>/dev/null || true
RUN cp -r sokit-cli-linux /output/ 2>/dev/null || true

# 设置输出目录
VOLUME /output
EOF

    # 构建镜像
    docker build -f Dockerfile.build -t sokit-builder .
    
    print_success "Docker镜像构建完成"
}

# 运行构建容器
run_build_container() {
    print_info "运行构建容器..."
    
    # 创建输出目录
    mkdir -p docker-output
    
    # 运行容器
    docker run --rm \
        -v "$(pwd):/workspace" \
        -v "$(pwd)/docker-output:/output" \
        sokit-builder
    
    print_success "构建容器运行完成"
}

# 清理临时文件
cleanup() {
    print_info "清理临时文件..."
    rm -f Dockerfile.build
    print_success "清理完成"
}

# 显示结果
show_results() {
    print_info "构建结果:"
    
    if [[ -d "docker-output" ]]; then
        echo "输出目录: docker-output/"
        ls -la docker-output/
        
        if [[ -f "docker-output/sokit-cli-linux-"*.tar.gz ]]; then
            print_success "Linux发布包已生成!"
            echo "文件位置: docker-output/sokit-cli-linux-*.tar.gz"
        fi
    else
        print_warning "未找到输出文件"
    fi
}

# 显示使用说明
show_usage() {
    echo ""
    echo "使用方法:"
    echo "  ./docker_build.sh [选项]"
    echo ""
    echo "选项:"
    echo "  --clean     清理临时文件"
    echo "  --help      显示此帮助信息"
    echo ""
    echo "支持的发行版:"
    echo "  - Ubuntu 20.04/22.04"
    echo "  - CentOS 8/9"
    echo "  - Arch Linux"
    echo "  - Debian 11"
    echo "  - 自定义发行版"
    echo ""
}

# 主函数
main() {
    case "${1:-}" in
        --clean)
            cleanup
            exit 0
            ;;
        --help|-h)
            show_usage
            exit 0
            ;;
        "")
            ;;
        *)
            print_error "未知选项: $1"
            show_usage
            exit 1
            ;;
    esac
    
    check_docker
    select_distro
    build_docker_image
    run_build_container
    cleanup
    show_results
    
    echo ""
    print_success "=========================================="
    print_success "Docker构建完成!"
    print_success "=========================================="
    echo "构建结果位于: docker-output/"
    echo ""
}

# 运行主函数
main "$@" 